<?php

class EmpAction extends Action {

    public $emps;

    public function __construct() {
        parent::__construct();
        $this->emps = M('employs');
        R("/Login/ifsession");
    }
	//显示
    public function emplist() {
        if (isset($_GET ['keyword'])) {
            $key = $this->_get('keyword');
			$username['username']=array('LIKE',"%".$key."%");
            $list = $this->emps->where($username)->select();
        } else {
            import("ORG.Util.Page");
            $count = $this->emps->count();
            $listrow = 15;
            $page = new Page($count, $listrow);
            $show = $page->show();
            $list = $this->emps->order('emp_id')->limit($page->firstRow . "," . $page->listRows)->select();
        }
        $this->assign('lists', $list);
        $this->show = $show;
        $this->display();
    }
	//修改
    public function edit() {
        if (IS_GET) {
            $id = I('emp_id');
            $emps = $this->emps->where('emp_id="'.$id.'"')->find();
			//var_dump($emps);die;
            $this->assign('emps',$emps);
			$this->display();
        }
        if (IS_POST) {
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 3145728 ;// 设置附件上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  = './Public/Uploads/'; // 设置附件上传目录
			//设置文件上传名(按照时间)  
            $upload->saveRule = "time";
			 //设置需要生成缩略图，仅对图像文件有效   
			$upload->thumb = true;   
			// 设置引用图片类库包路径   
			$upload->imageClassPath = 'ORG.Util.Image';   
			//设置需要生成缩略图的文件后缀   
			//$upload->thumbPrefix = 'm_';  //生产2张缩略图   
			//设置缩略图最大宽度   
			$upload->thumbMaxWidth = '150';   
			//设置缩略图最大高度   
			$upload->thumbMaxHeight = '150';   
			//设置上传文件规则   
			$upload->saveRule = uniqid;   
			//删除原图   
			$upload->thumbRemoveOrigin = true;   
			 if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
			 }else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			 }
			 // 保存表单数据 包括附件数据
			$img = $info[0]['savename']; 
			//旧的会员头像
            $data = I('post.');
			$data['emp_pic'] = $img; 
            if (empty($data ['password'])) {
                unset($data ['password']);
            } else {
                $data ['password'] = md5($data ['password']);
            }
            if ($this->emps->where('emp_id="'.$data ['id'].'"')->save($data)) {
				@unlink('./Public/Uploads/'.'thumb_'.$data['old_emp_pic']);
                $this->success('修改成功', U('Emp/emplist'));
            } else {
                $this->error('修改失败', U('Emp/emplist'));
            }
        }
    }
	//删除
    public function del($id) {
        if ($this->emps->where('emp_id="' . $id . '"')->delete()) {
            $this->success('删除成功', U('Emp/emplist'));
        } else {
            $this->error('删除失败', U('Emp/emplist'));
        }
    }
	//添加
    public function add() {
        if (IS_GET) {
            $this->display();
        }
        if (IS_POST) {
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 3145728 ;// 设置附件上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  = './Public/Uploads/'; // 设置附件上传目录
			//设置文件上传名(按照时间)  
            $upload->saveRule = "time";
			 //设置需要生成缩略图，仅对图像文件有效   
			$upload->thumb = true;   
			// 设置引用图片类库包路径   
			$upload->imageClassPath = 'ORG.Util.Image';   
			//设置需要生成缩略图的文件后缀   
			//$upload->thumbPrefix = 'm_';  //生产2张缩略图   
			//设置缩略图最大宽度   
			$upload->thumbMaxWidth = '150';   
			//设置缩略图最大高度   
			$upload->thumbMaxHeight = '150';   
			//设置上传文件规则   
			$upload->saveRule = uniqid;   
			//删除原图   
			$upload->thumbRemoveOrigin = true;   
			 if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
			 }else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			 }
			 // 保存表单数据 包括附件数据
			$img = $info[0]['savename'];  
			
            $data= I('post.');
			$data['emp_pic'] = $img; 
            $data ['password'] = md5($data ['password']);
            if ($this->emps->add($data)) {
                $this->success('添加成功', U('Emp/emplist'));
            } else {
                $this->error('添加失败', U('Emp/emplist'));
            }
        }
    }
	
    public function change() {
        if (IS_GET) {
            $this->display();
        }
        if (IS_POST) {

            $id = I('session.USERID');
            $password = I('post.password');
            $data ['password'] = md5($password);
            if (M("employs")->where("emp_id=" . $id)->save($data)) {
                $this->success('修改成功', U('Emp/change'));
            } else {
                echo M("employs")->getlastsql();
                die();
                $this->error('修改失败', U('Emp/change'));
            }
        }
    }

    public function find() {
        $username = I('post.loginid');
        $result = $this->emps->where('loginid="' . $loginid . '"')->find();
        if (!empty($result)) {
            echo '1';
        }
    }

}

?>